/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cliente;

import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.io.Serializable;

/**
 *
 * @author jyd
 */
/**
 * Class to represent a File object that can be sent
 * and recreated on another system
 * Will actually do the creating for you
 **/
public class Caja implements Serializable {

    // the file name I represent
    private String name;
    // the data in my file
    private byte[] data;

    public byte[] getData() {
        return data;
    }

    /**
		* Make a file packet that represents a given filename
		* @param name The filename this represents
		**/
    public Caja( String name ){
        this.name = name;
    }

	/**
	 * Get the name associated with this file
	 * @return The name
	 **/
    public String getName(){
        return name.split(";")[1];
    }

	/**
	 * Have the filepacket read iteself in from the
	 * file it represents in name
	 **/
    public void readIn(){
        try{
        File file = new File( name.split(";")[0]);
     	data = new byte[ (int)(file.length()) ];
            (new FileInputStream( file )).read( data );
        }catch( Exception e ){
            e.printStackTrace();
   	}
    }

	/**
	 * Have the file packet recreate itself, used
	 * after sending it to another location
	 * file will have same name and contents
	 * @param out The outputStream to write itself to
	 **/
	public void writeTo( OutputStream out  ){
  	try{
    	   out.write( data );
  	}catch( Exception e ){
   		e.printStackTrace();
   	}
	}
}
